package com.bicon.botu.medical.service.impl

import com.alibaba.fastjson.JSONObject
import com.baomidou.mybatisplus.plugins.Page
import com.baomidou.mybatisplus.service.impl.ServiceImpl
import com.bicon.botu.core.base.repository.redis.RedisHandler
import com.bicon.botu.medical.common.constant.FoodConstants
import com.bicon.botu.medical.dao.mapper.FoodMapper
import com.bicon.botu.medical.model.dto.FoodAppDTO
import com.bicon.botu.medical.model.dto.UnitAndRulerTypeDTO
import com.bicon.botu.medical.model.entity.Food
import com.bicon.botu.medical.model.vo.FoodUnitVO
import com.bicon.botu.medical.model.vo.MeasurementUnitVO
import com.bicon.botu.medical.service.ICustomFoodService
import com.bicon.botu.medical.service.IFoodService
import org.apache.commons.collections.CollectionUtils
import org.apache.commons.lang3.StringUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import java.util.ArrayList
import java.util.HashMap

/**
 * 食物Service实现类
 * @author 李双喜
 * @date 2018/7/18 11:48
 */
@Service
open class FoodServiceImpl : ServiceImpl<FoodMapper, Food>(), IFoodService {
    @Autowired
    open lateinit var iCustomFoodService: ICustomFoodService

    @Autowired
    open lateinit var foodMapper: FoodMapper

    @Autowired
    open lateinit var redisHandler: RedisHandler


    override fun searchFood(searchWord: String, pageNo: Int, pageSize: Int, userId: String): Page<FoodAppDTO> {
        var pageNo = pageNo
        //获取自定义食物
        val map = HashMap<String, String>(4)
        map["userId"] = userId
        map["name"] = searchWord
        var foodList = iCustomFoodService.getCustomFood(map)
        //获取系统食物并合并到一个集合返回
        var systemFoodList = this.getSystemFood(map)
        foodList.addAll(systemFoodList)
        //list分页
        val page = Page<FoodAppDTO>(pageNo, pageSize)
        if (pageNo <= 0) {
            pageNo = 1
        }
        //记录一下数据一共有多少条
        val total = foodList.size
        //实例化一个接受分页处理之后的数据
        val afterList = ArrayList<FoodAppDTO>()
        for (i in (pageNo - 1) * pageSize until if ((pageNo - 1) * pageSize + pageSize > total) total else (pageNo - 1) * pageSize + pageSize) {
            //然后将数据存入afterList中
            afterList.add(foodList[i])
        }
        //然后将处理后的数据集合进行返回
        page.total = total
        page.records = afterList
        return page
    }


    /**
     * 获取系统食物
     *
     * @param map
     * @return
     */
    override fun getSystemFood(map: Map<String, String>): MutableList<FoodAppDTO> {
        val systemFoodList = foodMapper.getSystemFood(map)
        if (!CollectionUtils.isEmpty(systemFoodList)) {
            systemFoodList.stream().forEach { foodAppDTO -> getSystemFoodUnitAndRulerType(foodAppDTO) }
        }
        return systemFoodList
    }


    /**
     * 获取系统食物单位和尺子类型
     *
     * @param foodAppDTO 食物对象
     */
    open fun getSystemFoodUnitAndRulerType(foodAppDTO: FoodAppDTO) {
        val unitAndRulerTypeDTOS = ArrayList<UnitAndRulerTypeDTO>()
        val redisTemplateUtilLocal = redisHandler.localRedisTemplate
        //获取该食物的计量单位
        val foodUnitVOJson = redisTemplateUtilLocal.getHash(FoodConstants.FOOD_UNIT_REDIS_KEY, "", foodAppDTO.id)
        if (StringUtils.isNotBlank(foodUnitVOJson)) {
            val foodUnitVOList = JSONObject.parseArray(foodUnitVOJson, FoodUnitVO::class.java)
            foodUnitVOList.stream().forEach({ foodUnitVO ->
                //查询计量单位名称
                val measurementUnitVO = JSONObject.parseObject(
                        redisTemplateUtilLocal.getHash(FoodConstants.MEASUREMENT_UNIT_REDIS_KEY, "", foodUnitVO.unitId), MeasurementUnitVO::class.java)
                val unitAndRulerTypeDTO = UnitAndRulerTypeDTO()
                unitAndRulerTypeDTO.unit = measurementUnitVO.name
                unitAndRulerTypeDTO.rulerType = "1"
                unitAndRulerTypeDTO.transformValue = foodUnitVO.unitValue.toString()
                unitAndRulerTypeDTOS.add(unitAndRulerTypeDTO)
            })
        }
        foodAppDTO.type = FoodConstants.SYSTEM_FOOD
        val unitAndRulerType = UnitAndRulerTypeDTO()
        unitAndRulerType.unit = "克"
        unitAndRulerType.rulerType = "2"
        unitAndRulerType.transformValue = "1"
        unitAndRulerTypeDTOS.add(unitAndRulerType)
        foodAppDTO.units = unitAndRulerTypeDTOS
    }

}
